package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@ApiOperation("菜品相关")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @PostMapping()
    @ApiOperation("新增菜品")
    public Result savewithFavor(@RequestBody DishDTO dishDTO){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishService.savewithFavor(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors =dishDTO.getFlavors();

        if(flavors != null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.batchinsert(flavors);
        }
        return Result.success();

    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        dishService.batchdelete(ids);
        return Result.success();

    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查菜品")
    public Result<DishVO> getbyid(@PathVariable Long id ){
        DishVO dishVO =  dishService.getbyidwithflavor(id);
        return Result.success(dishVO);
    }

    @PutMapping
     @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.updatewithflavor(dishDTO);
        dishFlavorMapper.delete(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.batchinsert(flavors);
        }
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品启售停售")
    public Result stoporstartdish(@PathVariable Integer status ,Long id){
        dishService.stoporstart(status , id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类ID查菜品")
    public Result<List<Dish>> querybycategoryid(Long categoryId){
        List<Dish> list = dishService.querybycategoryid(categoryId);
        return Result.success(list);
    }

}
